/*
 * Copyright (c) 2015  3 7
 * ---11 :23:27
 * ---作者:吴培基
 * ---QQ:757671834
 */

package kali.wpj.kali.util;

import android.content.Context;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by wpj on 2015/3/7.
 */
public class IPTools {
    /**
     * @see用来判断一个字符串是不是IP地址类型
     * @param address 待判断的字符串
     * @return true是false不是
     */

    public static boolean isIPAddress(String address){
        boolean flag = false;
        //192.168.0.100
        if(address.length()<7||address.length()>15||"".equals(address)){
                return flag;
        }

        String regex="\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b";
        Pattern pattern=Pattern.compile(regex);
        Matcher matcher=pattern.matcher(address);
        flag=matcher.find();
         return flag;

    }

    /**
     * @see用来判断用户是否在设置里面设置了IP地址
     * @param上下文
     * @return
     * @autor wpj
     */
    public static boolean haveHostIP(Context context){
     boolean flag=false;
        String tem=SharedTools.getString(context, "ip");
        if(tem==""||tem==null){
            flag=false;
        }else{
            flag=true;
        }
        return flag;
    }

    /**
     * 网址转换为
     * @param hostName 需要转换的网址 如www.fjnusoft.comm
     * @return IP如[218.66.79.151]
     */
    public static String getIPByHostName(String hostName){

        InetAddress address=null;
        try {
            address =InetAddress.getByName(hostName);
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return "未找到IP,请检查输入";
        }
        if(address==null){
            return "未找到IP,请检查输入";
        }
      String tem= address.getHostAddress().toString();
        return tem;
    }

    /**
     * 判断是否网址
     * @param web 待判断的地址
     * @return true or false
     */
    public static boolean isWeb(String web){

        boolean flag=false;
        String regex= "(http://|ftp://|https://|www){0,1}[^\u4e00-\u9fa5\\s]*?\\.(com|net|cn|me|tw|fr)[^\u4e00-\u9fa5\\s]*";
        Pattern pattern=Pattern.compile(regex);
        Matcher matcher=pattern.matcher(web);
        flag=matcher.find();
        return flag;

    }
}
